﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Wuqi.Webdiyer;

public partial class Controls_SearchControl : System.Web.UI.UserControl
{
    /// <summary>
    /// 当前页面呈现数据对应的数据表名

    /// </summary>
    public string DataTableName
    {
        get
        {
            string data = "";
            if (ViewState["Data"] != null)
            {
                data = ViewState["Data"].ToString();
            }
            return data;
        }
        set
        {
            ViewState["Data"] = value;
        }
    }
    public string FieldName
    {
        get
        {
            string fieldname = "";
            if (ViewState["Field"] != null)
            {
                fieldname = ViewState["Field"].ToString(); 
            }
            return fieldname;
        }
        set
        {
            ViewState["Field"] = value;
        }
    }
    public string PrimaryKeyColumnName
    {
        get
        {
            string columnName = "";
            if (ViewState["PrimaryKeyColumnName"] != null)
            {
                columnName = ViewState["PrimaryKeyColumnName"].ToString();
            }
            return columnName;
        }
        set
        {
            ViewState["PrimaryKeyColumnName"] = value;
        }
    }

    public string NameColumnName
    {
        get
        {
            string name = "";
            if (ViewState["NameColumnName"] != null)
            {
                name = ViewState["NameColumnName"].ToString();
            }
            return name;
        }
        set
        {
            ViewState["NameColumnName"] = value;
        }

    }
 
    public string UserControlId
    {
        get
        {
            string id = "";
            if (ViewState["UserControlId"] != null)
            {
                id = ViewState["UserControlId"].ToString();
            }
            return id;
        }
        set
        {
            ViewState["UserControlId"] = value;
        }
    }

    public string DataControlId
    {
        get
        {
            string id = "";
            if (ViewState["DataControlId"] != null)
            {
                id = ViewState["DataControlId"].ToString();
            }
            return id;
        }
        set
        {
            ViewState["DataControlId"] = value;
        }
    }

    public string PageControlId
    {
        get
        {
            string id = "";
            if (ViewState["PageControlId"] != null)
            {
                id = ViewState["PageControlId"].ToString();
            }
            return id;
        }
        set
        {
            ViewState["PageControlId"] = value;
        }
    }

    public bool IsSearchResult
    {
        get
        {
            bool isSearchResult = false;
            if (ViewState["IsSearchResult"] != null)
            {
                isSearchResult = Convert.ToBoolean(ViewState["IsSearchResult"]);
            }
            return isSearchResult;
        }
        set
        {
            ViewState["IsSearchResult"] = value;
        }
    }


    public string StrCondition
    {
        get
        {
            string strCondition = "";
            if (ViewState["StrCondition"] != null)
            {
                strCondition = ViewState["StrCondition"].ToString();
            }
            return strCondition;
        }
        set
        {
            ViewState["StrCondition"] = value;
        }

    }

    /// <summary>
    /// 初始化参数

    /// </summary>
    /// <param name="pageSize">每页显示记录数</param>
    /// <param name="pageIndex">页码</param>
    /// <param name="dataTableName">表名</param>
    /// <param name="primaryKeyColumnName">主键名</param>
    /// <param name="nameColumnName">按名称搜索的列名</param>
    /// <param name="userControlId">用户控件ID</param>
    /// <param name="dataControlId">数据控件ID</param>
    public void InitParam(string dataTableName, string primaryKeyColumnName,string nameColumnName, string userControlId, string dataControlId,string pageControlId)
    {
        this.DataTableName = dataTableName;
        this.PrimaryKeyColumnName = primaryKeyColumnName;
        this.NameColumnName = nameColumnName;
        this.UserControlId = userControlId;
        this.DataControlId = dataControlId;
        this.PageControlId = pageControlId;
    }
    /// <summary>
    /// 初始化参数


    /// </summary>
    /// <param name="pageSize">每页显示记录数</param>
    /// <param name="FieldName">查询的列字段</param>
    /// <param name="pageIndex">页码</param>
    /// <param name="dataTableName">表名</param>
    /// <param name="primaryKeyColumnName">主键名</param>
    /// <param name="nameColumnName">按名称搜索的列名</param>
    /// <param name="userControlId">用户控件ID</param>
    /// <param name="dataControlId">数据控件ID</param>
    public void InitParam(string dataTableName, string FieldName,string primaryKeyColumnName, string nameColumnName, string userControlId, string dataControlId, string pageControlId)
    {
        this.DataTableName = dataTableName;
        this.PrimaryKeyColumnName = primaryKeyColumnName;
        this.NameColumnName = nameColumnName;
        this.UserControlId = userControlId;
        this.DataControlId = dataControlId;
        this.PageControlId = pageControlId;
        this.FieldName = FieldName;
    }
    /// <summary>
    /// 初始化参数

    /// </summary>
    /// <param name="pageSize">每页显示记录数</param>
    /// <param name="pageIndex">页码</param>
    /// <param name="dataTableName">表名</param>
    /// <param name="primaryKeyColumnName">主键名</param>
    /// <param name="nameColumnName">按名称搜索的列名</param>
    /// <param name="dataControlId">数据控件ID</param>
    public void InitParam(string dataTableName, string primaryKeyColumnName, string nameColumnName, string dataControlId, string pageControlId)
    {
        this.DataTableName = dataTableName;
        this.PrimaryKeyColumnName = primaryKeyColumnName;
        this.NameColumnName = nameColumnName;
        this.DataControlId = dataControlId;
        this.PageControlId = pageControlId;
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (this.UserControlId != "")
        {
            UserControl dataUserControl = (UserControl)this.Page.FindControl(this.UserControlId);
            AspNetPager pager = (AspNetPager)this.Page.FindControl(this.PageControlId);
            if (dataUserControl != null && pager!=null)
            {
                pager.CurrentPageIndex = 1;
                Repeater rpt = (Repeater)dataUserControl.FindControl(this.DataControlId);
                if (rpt != null && this.DataTableName != "" && this.PrimaryKeyColumnName != "")
                {
                    int pageCount = 0, count = 0;
                    string strSql = string.Empty;
                    if (dropSearchType.SelectedValue.ToLower() == "code")
                    {
                        this.StrCondition = "AND " + this.PrimaryKeyColumnName + " LIKE '%" + txtKeyWord.Text.Trim() + "%'";
                        if (FieldName != "")
                        {
                            BindParentPageControl(rpt, pager, this.DataTableName,FieldName, this.PrimaryKeyColumnName, this.StrCondition, pager.PageSize, 1, out pageCount, out count, out strSql);
                        }
                        else
                        {
                            BindParentPageControl(rpt, pager, this.DataTableName, this.PrimaryKeyColumnName, this.StrCondition, pager.PageSize, 1, out pageCount, out count, out strSql);
                        }
                    }
                    else if (dropSearchType.SelectedValue.ToLower() == "name")
                    {
                        this.StrCondition = "AND " + this.NameColumnName + " LIKE '%" + txtKeyWord.Text.Trim() + "%'";
                        if (FieldName != "")
                        {
                            BindParentPageControl(rpt, pager, this.DataTableName, FieldName, this.PrimaryKeyColumnName, this.StrCondition, pager.PageSize, 1, out pageCount, out count, out strSql);
                        }
                        else
                        {
                            BindParentPageControl(rpt, pager, this.DataTableName, this.PrimaryKeyColumnName, this.StrCondition, pager.PageSize, 1, out pageCount, out count, out strSql);
                        }
                    }
                }
                else
                {
                    this.IsSearchResult = false;
                    return;
                }
            }
            else
            {
                this.IsSearchResult = false;
                return;
            }


        }
        else
        {
            this.IsSearchResult = false;
            return;
        }
    }

    /// <summary>
    /// 绑定母页的控件

    /// </summary>
    /// <param name="control"></param>
    /// <param name="dataTableName"></param>
    /// <param name="primaryKeyName"></param>
    /// <param name="strCondition"></param>
    /// <param name="pageSize"></param>
    /// <param name="pageIndex"></param>
    /// <param name="pageCount"></param>
    /// <param name="count"></param>
    /// <param name="strSql"></param>
    public void BindParentPageControl(Repeater rptControl,AspNetPager pageControl, string dataTableName, string primaryKeyName, string strCondition, int pageSize, int pageIndex, out int pageCount, out int count, out string strSql)
    {
        rptControl.DataSource = BLL.SearchControlBuess.Search(dataTableName, primaryKeyName, strCondition, pageSize, pageIndex, out pageCount, out count, out strSql);
        rptControl.DataBind();
        pageControl.RecordCount = count;  
        this.IsSearchResult = true; //表示显示结果为查询结果

    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="rptControl">控件名</param>
    /// <param name="pageControl">分页控件</param>
    /// <param name="dataTableName">数据库表名</param>
    /// <param name="primaryKeyName">数据库表主键</param>
    /// <param name="strCondition">查询条件</param>
    /// <param name="pageSize">每页显示数量</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageCount">返回总页数</param>
    /// <param name="count">返回记录数</param>
    /// <param name="strSql">返回SQL语句</param>
    public void BindParentPageControl(Repeater rptControl, AspNetPager pageControl, string dataTableName,string FieldName, string primaryKeyName, string strCondition, int pageSize, int pageIndex, out int pageCount, out int count, out string strSql)
    {
        rptControl.DataSource = BLL.SearchControlBuess.Search(dataTableName,FieldName,primaryKeyName, strCondition, pageSize, pageIndex, out pageCount, out count, out strSql);
        rptControl.DataBind();
        pageControl.RecordCount = count;
        this.IsSearchResult = true; //表示显示结果为查询结果

    }
}
